package com.fifedu.lib_common_utils.permission;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.fifedu.lib_common_utils.ContextProvider;
import com.fifedu.lib_common_utils.R;
import com.fifedu.lib_common_utils.SystemUtil;
import com.fifedu.lib_common_utils.dialog.BaseDialogCallBack;
import com.fifedu.lib_common_utils.dialog.BaseDialogUtils;
import com.fifedu.lib_common_utils.log.LogUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;

import java.util.Arrays;
import java.util.List;

/**
 * 权限申请
 */

public class PermissionUtils {

    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;//相机权限单独判断
    public static final String PERMISSION_AUDIO = Manifest.permission.RECORD_AUDIO;//录音权限强制获取
    public static final String PERMISSION_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;//录音权限强制获取
    public static final String PERMISSION_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_BLUETOOTH = Manifest.permission.BLUETOOTH;
    // 从Android 12开始，过去的蓝牙权限被拆分成了3个新的权限，并且全都是运行时权限（需要动态申请）：
    // 用于使用蓝牙扫描附件其他的蓝牙设备
    public static final String PERMISSION_BLUETOOTH_CONNECT = Manifest.permission.BLUETOOTH_CONNECT;
    //用于允许当前的设备被其他的蓝牙设备所发现
    public static final String PERMISSION_BLUETOOTH_ADVERTISE = Manifest.permission.BLUETOOTH_ADVERTISE;
    //用于使用蓝牙扫描附件其他的蓝牙设备
    public static final String PERMISSION_BLUETOOTH_SCAN = Manifest.permission.BLUETOOTH_SCAN;
    public static final String AUTHORITY_NOTICE = "authority_notice";//通知权限

    public static final String[] PERMISSIONS_ALL = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            AUTHORITY_NOTICE
    };
    public static final String[] PERMISSIONS_VIDEO = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    public static final String[] PERMISSIONS_VIDEO_SIMPLE = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
    public static final String[] PERMISSIONS_CAMERA = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    public static final String[] PERMISSIONS_CAMERA_SIMPLE = {Manifest.permission.CAMERA};
    public static final String[] PERMISSIONS_FILE = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    public static final String[] PERMISSIONS_FILE_R = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.MANAGE_EXTERNAL_STORAGE};
    private static final boolean isEqualOrAboveVersion = SystemUtil.isEqualOrAboveVersion(Build.VERSION_CODES.R);
    public static final String[] PERMISSIONS_MANAGE_FILE = !isEqualOrAboveVersion ? PERMISSIONS_FILE : PERMISSIONS_FILE_R;
    public static final String[] PERMISSIONS_RECORD = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    public static final String[] PERMISSIONS_RECORD_SIMPLE = {Manifest.permission.RECORD_AUDIO};
    public static String[] PERMISIONS_LOCATION = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.CHANGE_WIFI_STATE};
    public static String[] PERMISIONS_LOCATION_SIMPLE = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
    public static String PERMISSION_CAMERA_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_camera_des);
    public static String PERMISSION_AUDIO_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_audio_des);
    public static String PERMISSION_VIDEO_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_video_des);
    public static String PERMISSION_STORAGE_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_storage_des);
    public static String PERMISSION_LOCATION_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_location_des);
    public static String PERMISSION_PHONE_STATE_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_phone_state_des);
    public static String PERMISSION_BLUETOOTH_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_bluetooth_des);

    public static String PERMISSION_COMMON_DES = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_common_des);
    public static final String CAMERA = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_camera);
    public static final String AUDIO = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_audio);
    public static final String VIDEO = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_video);
    public static final String STORAGE = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_storage);
    public static final String LOCATION = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_location);
    public static final String PHONE_STATE = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_location);
    public static final String BLUETOOTH = ContextProvider.getAppContext().getString(R.string.lib_utils_perm_bluetooth);
    public static boolean isShowingPermissionDialog = false; // 是否正在展示权限弹窗

    private static void delayResetDialogStatus() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isShowingPermissionDialog = false;
            }
        }, 1000);
    }

    /**
     * 请求权限
     *
     * @param activity fragmentActivity
     * @param isExit   是否退出当前页面
     */
    public static void requestPermissions(final Activity activity, final boolean isExit, final PermissionCallBack permissionCallBack, final String... permissions) {
        if (permissions == null || permissions.length == 0 || isShowingPermissionDialog) {
            return;
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                try {
                    isShowingPermissionDialog = true;
                    boolean isAllPermissionsGranted = checkPermissionsIsGranted(activity, permissions);
                    if (isAllPermissionsGranted) {
                        isShowingPermissionDialog = false;
                        permissionCallBack.onGranted();
                    } else {
                        String title = activity.getString(R.string.lib_utils_perm_request_title);
                        String content = null;
                        List<String> permissionList = Arrays.asList(permissions);
                        if (permissionList.contains(PERMISSION_CAMERA) && permissionList.contains(PERMISSION_AUDIO)) {
                            content = PERMISSION_VIDEO_DES;
                        } else if (permissionList.contains(PERMISSION_CAMERA)) {
                            content = PERMISSION_CAMERA_DES;
                        } else if (permissionList.contains(PERMISSION_AUDIO)) {
                            content = PERMISSION_AUDIO_DES;
                        } else if (permissionList.contains(PERMISSION_STORAGE)) {
                            content = PERMISSION_STORAGE_DES;
                        } else if (permissionList.contains(PERMISSION_LOCATION)) {
                            content = PERMISSION_LOCATION_DES;
                        } else if (permissionList.contains(PERMISSION_PHONE_STATE)) {
                            content = PERMISSION_PHONE_STATE_DES;
                        } else if (isNeedBluetoothPerm(permissionList)) {
                            content = PERMISSION_BLUETOOTH_DES;
                        } else {
                            content = PERMISSION_COMMON_DES;
                        }
                        String confirm = activity.getString(R.string.lib_utils_confirm);
                        String cancel = activity.getString(R.string.lib_utils_cancel);
                        BaseDialogUtils.createCommonDialog(activity, title, content, confirm, cancel, BaseDialogUtils.DIALOG_SHOWTYPE_NORMAL, false, new BaseDialogCallBack() {
                            @Override
                            public void onConfirm() {
                                realRequestPermissions(activity, permissionCallBack, isExit, permissions);
                            }

                            @Override
                            public void onCancel() {
                                permissionCallBack.onDenied();
                                delayResetDialogStatus();
                                if (isExit) activity.finish();
                            }
                        });
                    }
                } catch (Throwable e) {
                }
            }
        });


    }

    private static void realRequestPermissions(final Activity activity, final PermissionCallBack permissionCallBack, final boolean isExit, final String... permissions) {
        try {
            XXPermissions.with(activity)
                    // 申请多个权限
                    .permission(permissions)
                    // 设置权限请求拦截器（局部设置）
                    //.interceptor(new PermissionInterceptor())
                    // 设置不触发错误检测机制（局部设置）
                    //.unchecked()
                    .request(new OnPermissionCallback() {

                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            if (!allGranted) {
                                //只要有一个选择：禁止，但没有选择“以后不再询问”，以后申请权限，会继续弹出提示
                                if (permissionCallBack != null) {
                                    if (isExit) {
                                        activity.finish();
                                    } else {
                                        permissionCallBack.onDenied();
                                    }
                                }
                            } else {
                                //全部同意后调用
                                if (permissionCallBack != null) {
                                    permissionCallBack.onGranted();
                                }
                            }
                            delayResetDialogStatus();
                        }

                        @Override
                        public void onDenied(@NonNull List<String> permissionList, boolean doNotAskAgain) {
                            if (permissionCallBack != null) {

                                String content = "";
                                if (permissionList.contains(PERMISSION_CAMERA) && permissionList.contains(PERMISSION_AUDIO)) {
                                    content = VIDEO;
                                } else if (permissionList.contains(PERMISSION_CAMERA)) {
                                    content = CAMERA;
                                } else if (permissionList.contains(PERMISSION_AUDIO)) {
                                    content = AUDIO;
                                } else if (permissionList.contains(PERMISSION_STORAGE)) {
                                    content = STORAGE;
                                } else if (permissionList.contains(PERMISSION_LOCATION)) {
                                    content = LOCATION;
                                } else if (permissionList.contains(PERMISSION_PHONE_STATE)) {
                                    content = PHONE_STATE;
                                } else if (isNeedBluetoothPerm(permissionList)) {
                                    content = BLUETOOTH;
                                }
                                if (doNotAskAgain) {
                                    PermissionDialog.showPermissionDialog(activity, content, isExit);
                                    permissionCallBack.onDeniedRational();
                                }
                            }
                            delayResetDialogStatus();
                        }
                    });

        } catch (Exception e) {
            if (permissionCallBack != null) {
                LogUtils.e("requestPermissions==", e.getMessage() + "===");
                permissionCallBack.onException();
            }
            delayResetDialogStatus();
        }
    }

    private static boolean isNeedBluetoothPerm(List<String> permissions) {
        return permissions.contains(PERMISSION_BLUETOOTH_CONNECT) || permissions.contains(PERMISSION_BLUETOOTH_SCAN) || permissions.contains(PERMISSION_BLUETOOTH_ADVERTISE);
    }

    /**
     * 判断是否有录音权限（只检测录音权限）
     */
    public static boolean isHasAudioPermission(Activity activity) {
        return checkPermissionsIsGranted(activity, PERMISSION_AUDIO);
    }

    /**
     * 检查某个权限是否被申请
     */
    public static boolean checkPermissionsIsGranted(Activity activity, String... permissions) {
        return XXPermissions.isGranted(activity, permissions);
    }
}